package com.yjetms.intercepter;

import com.sun.deploy.net.HttpResponse;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CheckSessionAndDel {
    public static boolean checkSession(HttpServletRequest rqs){
        HttpSession newsession=rqs.getSession();
        String username=newsession.getAttribute("LoginUsername").toString();

        ServletContext servletContext=rqs.getServletContext();
        String newSessionId=newsession.getId().toString();

        Object oldSessionId=servletContext.getAttribute(username);

        System.out.println("new:"+newSessionId+"  old:"+oldSessionId);
        if(oldSessionId==null){
            return true;
        }else if(newSessionId.equals(oldSessionId.toString())){
            System.out.println("两个相同");
            return true;
        }else {
            return false;
        }
    }
    public static void delSecondUser(HttpServletRequest rqs, HttpServletResponse rps){
        HttpSession session=rqs.getSession();
        session.removeAttribute("LoginUsername");
        session.setAttribute("loginState","1");
    }
}
